package cn.felord.debezium.utils;

import java.nio.charset.Charset;

import static cn.felord.debezium.utils.TcpClientPusDuan.hexStringToByteArray;

/**
 * 中文转 16进制 ——————————————————  GB2312
 */
public class ChineseToHex {
    // 将中文字符串转换为16进制表示的函数
    public static String chineseToHex(String chineseStr) {
        // 使用UTF-8编码将字符串转换为字节数组
        Charset gb2312charset = Charset.forName("GB2312");
        byte[] bytes = chineseStr.getBytes(gb2312charset);
        StringBuilder hexString = new StringBuilder();
        // 遍历字节数组，将每个字节转换为16进制字符串
        for (byte b : bytes) {
            // 将字节转换为无符号的整数，并格式化为两位16进制字符串
            String hex = String.format("%02x", b & 0xFF);
            hexString.append(hex);
        }
        // 返回拼接后的16进制字符串
        return hexString.toString();
    }
    public static String hexToCHinise(byte[] bytess){
        String chineseString = new String(bytess, Charset.forName("GB2312"));
        return chineseString;
    }

    public static void main(String[] args) {
//        String chineseStr = "改装盘HCCM";
//        String hex = chineseToHex(chineseStr);
//        System.out.println("中文字符串的16进制表示: " + hex);
//        System.out.println(chineseToHex("GG20HC"));
//        System.out.println(chineseToHex("改装盘HCCM"));
//        String a = chineseToHex("改装盘HCCM");
//        System.out.println(a.length()/2);
//        System.out.println(String2AcsII16.int10to16_2(a.length()/2));
//        System.out.println(chineseToHex("普通G3000 GG20"));   //C6 D5 CD A8 47 33 30 30 30 20 47 47 32 30
//        System.out.println(chineseToHex("样品G3000 GG20"));   //C6 D5 CD A8 47 33 30 30 30 20 47 47 32 30
//        System.out.println(chineseToHex("G3000"));   //C6 D5 CD A8 47 33 30 30 30 20 47 47 32 30
//        String cz = "改装盘HCCM";
//        String czh = "00 01 00 00 00 1B 01 10 00 00 00 08";
//        String cz16 = chineseToHex(cz);
//        String l = String2AcsII16.int10to16_2(cz16.length() / 2);
//        String czData = czh + l + cz16;
//        System.out.println(czData);
        System.out.println(chineseToHex("样品G3000 GG20"));
        System.out.println(hexToCHinise(hexStringToByteArray(chineseToHex("GB2312"))));

    }
}